﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace QuickFramework.SqlSugarOrm
{
    public partial class Repository<T> : IRepository<T> where T: Shared.Contracts.EntityBase,new ()
    {
        /// <summary>
        /// 数据库链接节点名称
        /// </summary>
        private string m_ConnectionStringName;
        /// <summary>
        /// 数据库链接名
        /// </summary>
        public string ConnectionStringName
        {
            get { return m_ConnectionStringName; }
        }
        /// <summary>
        /// 是否全部使用主库资源
        /// </summary>
        private bool m_IsMaster;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectionStringName">数据库连接名</param>
        /// <param name="isMaster">是否全部使用主库资源</param>
        public Repository(string connectionStringName, bool isMaster = false)
        {
            m_ConnectionStringName = connectionStringName;
            m_IsMaster = isMaster;
        }
        /// <summary>
        /// 仓储内部使用的工作单元
        /// </summary>
        public IUnitOfWork UnitOfWork
        {
            get
            {
                return new UnitOfWork().CreateUoW(m_ConnectionStringName, m_IsMaster);
            }
        }
        /// <summary>
        /// ADO 对象
        /// </summary>
        public IAdo Ado(IUnitOfWork unitOfWork = null)
        {
            IUnitOfWork uow = unitOfWork;
            if (null == unitOfWork)
            {
                uow = this.UnitOfWork;
            }
            return uow.SqlSugarClient.Ado;
        }
        /// <summary>
        /// SqlSugarClient 对象
        /// </summary>
        public SqlSugarClient SqlSugarClient(IUnitOfWork unitOfWork = null)
        {
            IUnitOfWork uow = unitOfWork;
            if (null == unitOfWork)
            {
                uow = this.UnitOfWork;
            }
            return this.UnitOfWork.SqlSugarClient;
        }
        /// <summary>
        /// SqlSugarClient 对象
        /// </summary>
        public SimpleClient<T> SimpleClient(IUnitOfWork unitOfWork = null)
        {
            IUnitOfWork uow = unitOfWork;
            if (null == unitOfWork)
            {
                uow = this.UnitOfWork;
            }
            return this.UnitOfWork.SqlSugarClient.GetSimpleClient<T>();
        }
        /// <summary>
        /// 根据对象设定,更新需要修改的指定栏位
        /// </summary>
        /// <param name="UpdateObj"></param>
        /// <param name="unitOfWork"></param>
        /// <returns></returns>
        public IUpdateable<T> Updateable4Setting(T UpdateObj, IUnitOfWork unitOfWork = null)
        {
            IUnitOfWork uow = unitOfWork;
            if (null == unitOfWork)
            {
                uow = this.UnitOfWork;
            }
            return this.UnitOfWork.SqlSugarClient.Updateable<T>(UpdateObj).UpdateColumns(UpdateObj.PropertyChanges.ToArray());
        }
    }
}
